﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class RushSweepRewardItem : RushSweepItemBase 
    {
        public UILabel additionLabel;
        public Transform grid;
        public GameObject itemPrefab;

        List<GameObject> items = new List<GameObject>();

        public int colLimit = 4;
        public float cellWidth = 100f;
        public float cellHeight = 100f;


        public override float GetHeight()
        {
            return height;
        }
        public float height
        {
            get {
                int rowNum = items.Count / colLimit;
                if (items.Count % colLimit != 0)
                {
                    rowNum++;
                }

                return cellHeight * rowNum + 42f;
            }
        }

        public void Init(ProtoBuf.Message.ResultObject gain, string addition)
        {
            SetAddition(addition);
            Clear();
            foreach (var info in gain.wars)
            {
                var obj = GameObject.Instantiate(itemPrefab) as GameObject;
                obj.GetComponent<CommonIcon>().SetData(info.id, false);
                obj.transform.SetParent(grid.transform);
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = Vector3.one;
                obj.transform.localEulerAngles = Vector3.zero;
                items.Add(obj);
            }
            foreach (var info in gain.equips)
            {
                var obj = GameObject.Instantiate(itemPrefab) as GameObject;
                obj.GetComponent<CommonIcon>().SetData(info.equipId, false);
                obj.transform.SetParent(grid.transform);
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = Vector3.one;
                obj.transform.localEulerAngles = Vector3.zero;
                items.Add(obj);
            }
            foreach (var info in gain.treas)
            {
                var obj = GameObject.Instantiate(itemPrefab) as GameObject;
                obj.GetComponent<CommonIcon>().SetData(info.treasureId, false);
                obj.transform.SetParent(grid.transform);
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = Vector3.one;
                obj.transform.localEulerAngles = Vector3.zero;
                items.Add(obj);
            }
            foreach (var info in gain.simps)
            {
                if (info.num == 0)
                    continue;
                var obj = GameObject.Instantiate(itemPrefab) as GameObject;
                obj.GetComponent<CommonIcon>().SetData(info.id, false, info.num);
                obj.transform.SetParent(grid.transform);
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = Vector3.one;
                obj.transform.localEulerAngles = Vector3.zero;
                items.Add(obj);
            }

            for (int i = 0; i < items.Count; ++i)
            {
                items[i].transform.SetParent(grid.transform);
                items[i].transform.localPosition = new Vector3(i % colLimit * cellWidth, i / colLimit * cellHeight, 0f);
                items[i].transform.localScale = Vector3.one;
                items[i].transform.localEulerAngles = Vector3.zero;
            }
        }

        void SetAddition(string addition)
        {
            var arr = addition.Split(new char[1] { ','}, System.StringSplitOptions.RemoveEmptyEntries);
            if (arr.Length == 2)
            {
                float val = float.Parse(arr[1]);
                string attr = RushPage.ParseAttributeAdditionName(arr[0]); 
                string addStr = string.Format("+{0}%" + attr, (val * 100).ToString("f2"));
                additionLabel.text = string.Format(TableTextBlo.GetText("rush_addition_result"), addStr);
            }
            else
            {
                additionLabel.text = "";
            }
        }

        void Clear()
        {
            foreach (var item in items)
            {
                GameObject.Destroy(item);
            }
            items.Clear();
        }

    }
}